
//
//  HJFMDBDataSoure.m
//  project
//
//  Created by jc on 2019/4/24.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJFMDBDataSoure.h"

@implementation HJFMDBDataSoure

+(instancetype)instance {
    static HJFMDBDataSoure * soucre = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        soucre = [[self alloc]init];
    });
    return soucre;
}

-(void)configFMDB {
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"mySqlite.db"];
    LKLog(@"%@",path);
    FMDatabase * dataBass = [FMDatabase databaseWithPath:path];
    if ([dataBass open]) {
        NSString * sqlite = @"CREATE TABLE IF NOT EXISTS t_Trip (jsonStr text);";
        if ([dataBass executeUpdate:sqlite]) {
            LKLog(@"打开数据库成功");
        }else {
            LKLog(@"打开数据库失败");
        }
    }
    self.db = dataBass;
    [dataBass close];
}

-(void)findFMDBWithData:(void(^)(NSMutableArray *dataArray))data {
    if ([self.db open]) {
        NSMutableArray * arr = [NSMutableArray array];
        FMResultSet * resultSet = [self.db executeQuery:@"SELECT * FROM t_Trip"];
        while ([resultSet next]) {
            NSString * jsonStr = [resultSet objectForColumn:@"jsonStr"];
            LKLog(@"jsonStr==%@",jsonStr);
            [arr addObject:jsonStr];
            if (data) {
                data(arr);
            }
        }
    }
    [self.db close];
}

-(void)insertFMDBWithlist:(NSString *)jsonStr {
    if ([self.db open]) {
        if ([self.db executeUpdateWithFormat:@"INSERT INTO t_Trip (jsonStr) VALUES (%@);", jsonStr]) {
            LKLog(@"插入数据成功");
        }else {
            LKLog(@"插入数据失败");
        }
    }
    [self.db close];
}

- (void)delete {
    [self.db executeUpdate:@"DROP TABLE IF EXISTS t_Trip;"];
    [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_Trip (id integer PRIMARY KEY AUTOINCREMENT, jsonStr text NOT NULL);"];
    [self.db close];
}


@end
